package com.glory.erp.generator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Description: TODO(用一句话描述该文件做什么)
 * @Title: TranposUtils.java
 * @Author: 樊双峰
 * @Date: 2018年7月24日 上午10:31:49
 * @Version V1.0
 */
public class TranposUtils {

	public static ArrayList<HashMap> colToRow(String title, String value,
			ArrayList<Map> al) {
		// 定义一个MAP
		HashMap<String, Integer> sortRecode = new HashMap<String, Integer>();
		int rows = 0;
		ArrayList<String> headList = new ArrayList<String>();
		ArrayList<HashMap> resultList = new ArrayList<HashMap>();
		boolean readHead = true;
		for (Map mp : al) {
			// readHead为True执行下步
			if (readHead) {
				for (Iterator s = mp.keySet().iterator(); s.hasNext();) {
					String temp = s.next().toString();
					if (!temp.equalsIgnoreCase(title)) {
						if (!temp.equalsIgnoreCase(value)) {
							headList.add(temp);
						}
					}
				}
				readHead = false;
			}

			String key = "";
			HashMap tempMap = new HashMap();
			for (String p : headList) {
				key = key + mp.get(p);
				tempMap.put(p, mp.get(p));
			}
			if (sortRecode.containsKey(key)) {
				resultList.get(sortRecode.get(key)).put(mp.get(title),
						mp.get(value));
			} else {
				tempMap.put(mp.get(title), mp.get(value));
				resultList.add(tempMap);
				sortRecode.put(key, new Integer(rows));
				rows++;
			}
		}
		return resultList;
	}
	
	public static void main(String[] args) {
        HashMap hm = new HashMap();
        ArrayList<Map> al = new ArrayList<Map>();
        hm.put("姓名", "张三");
        hm.put("学校", "中国大学");
        hm.put("成绩", "80");
        hm.put("性别", "女");
        al.add(hm);
        hm = new HashMap();
        hm.put("姓名", "李四");
        hm.put("学校", "中国大学");
        hm.put("成绩", "89");
        hm.put("性别", "男");
        al.add(hm);
        hm = new HashMap();
        hm.put("姓名", "王二麻子");
        hm.put("学校", "中国大学");
        hm.put("成绩", "60");
        hm.put("性别", "女");
        al.add(hm);
        System.out.println(al);
        System.out.println(TranposUtils.colToRow("姓名", "成绩", al));
        for (HashMap mp : TranposUtils.colToRow("姓名", "成绩", al)) {
            System.out.println(mp);
        }
    }
}